package ee.rsi2m.goproject.util;

import ee.rsi2m.goproject.model.GoBoard;
import ee.rsi2m.goproject.model.GoStone;
import ee.rsi2m.goproject.service.SurfaceViewThread;
import android.graphics.Paint;
import android.util.Log;

public class BoardLoader {
	private static int boardSize;

	private static int getBoardSize(String[] input) {
		assert input.length > 0;
		if (input[2].charAt(1) != '.') {
			return Integer.parseInt("" + input[2].charAt(0) + input[2].charAt(1));
		} else {
			return Integer.parseInt("" + input[2].charAt(0));
		}

	}

	private static void createBinaryBoardMatrix(SurfaceViewThread thread,String[] input,GoBoard board) {
		
		

		
		
		int count = 0;
		int[][] matrix=new int[getBoardSize(input)][getBoardSize(input)];
		for (String string : input) {
			// Log.w("BoardLoader", "String kastrated" + string.indexOf("."));
			if (string.length() > boardSize && string.indexOf(".")!=-1) {
				int first = string.indexOf(".");
				String k = string.substring(first, string.indexOf(".") + getBoardSize(input)-1);
				Log.w("BoardLoader", "String kastrated" + k);
				for(int i=0;i<k.length();i++){
					if(k.charAt(i)=='.'){
						matrix[count][i]=0;
					}else if(k.charAt(i)=='X'){
						Log.w("BoardLoader", "X:"+(getBoardSize(input)-i)+"Y:"+(getBoardSize(input)-count));
						matrix[count][i]=1;
						
						Point point = new Point(getBoardSize(input)-i,getBoardSize(input)-count);
						GoStone stone = board.boardState[(int) point.getX()][(int) point.getY()];
						GoStone gstone = new GoStone(new Point(stone.getX(), stone.getY()), true, thread.paint);
							gstone.initialize(Constants.BLACK, board.getBlackStone(), board.getTurnCount());


						gstone.setRelativeX((int) point.getX());
						gstone.setRelativeY((int) point.getY());

						board.boardState[gstone.getRelativeX()][gstone.getRelativeY()] = gstone;
						Log.w("BoardLoader","Color BLACK"+(gstone.getTurn()));

						thread.addStoneToArray(gstone);
					}else if(k.charAt(i)=='O'){
						Log.w("BoardLoader", "X:"+(getBoardSize(input)-i)+"Y:"+(getBoardSize(input)-count));
						matrix[count][i]=2;
						
						Point point = new Point(getBoardSize(input)-i,getBoardSize(input)-count);
						GoStone stone = board.boardState[(int) point.getX()][(int) point.getY()];
						GoStone gstone = new GoStone(new Point(stone.getX(), stone.getY()), true, thread.paint);
						gstone.initialize(Constants.WHITE, board.getWhiteStone(), board.getTurnCount());


						gstone.setRelativeX((int) point.getX());
						gstone.setRelativeY((int) point.getY());

						board.boardState[gstone.getRelativeX()][gstone.getRelativeY()] = gstone;
						Log.w("BoardLoader","Color WHITE"+(gstone.getTurn()));
						thread.addStoneToArray(gstone);
					}
				}
				count++;
			}
		}
	}

	private static void analyzeInput(String[] input) {
		Log.w("BoardLoader", "ABCS" + Constants.ALPHABETICALCOORDINATES.length());
		for (String string : input) {
			Log.w("BoardLoader", "String lenght:" + string.length());
		}
	}

	public static int getInputStringLength(String input) {
		StringBuffer temp = new StringBuffer(input);
		for (int i = 0; i < temp.length(); i++) {
			if (temp.charAt(i) == ' ') {
				temp.deleteCharAt(i);
			}
		}
		// temp.replace(temp.indexOf("WHITE(O)hascaptured0stones"),
		// temp.indexOf("WHITE(O)hascaptured0stones") +
		// "WHITE(O)hascaptured0stones".length(), "");
		// temp.replace(temp.indexOf("BLACK(X)hascaptured0stones"),
		// temp.indexOf("BLACK(X)hascaptured0stones") +
		// "BLACK(X)hascaptured0stones".length(), "");
		Log.w("BoardLoader", "" + temp.length());
		Log.w("BoardLoader", "" + temp.toString());

		String[] inputArray = temp.toString().split("\n");
		boardSize = getBoardSize(inputArray);
		//createBinaryBoardMatrix(inputArray);
		Log.w("BoardLoader", "" + temp.toString().split("\n").length);
		Log.w("BoardLoader", "Board size:" + getBoardSize(inputArray));

		return input.length();
	}

	public static void loadBoad(SurfaceViewThread thread, GoBoard board, String GTPString) {
		StringBuffer temp = new StringBuffer(GTPString);
		for (int i = 0; i < temp.length(); i++) {
			if (temp.charAt(i) == ' ') {
				temp.deleteCharAt(i);
			}
		}
		String[] inputArray = temp.toString().split("\n");
		boardSize = getBoardSize(inputArray);
		createBinaryBoardMatrix(thread,inputArray,board);
		
	}
}
